package com.lwm.controller;

import com.github.pagehelper.PageInfo;
import com.lwm.domain.Device;
import com.lwm.domain.ResponseResult;
import com.lwm.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备信息控制层
 */
@RestController
@RequestMapping("/device")
@Slf4j
public class DeviceController {
    @Resource
    private DeviceService deviceService;

    /**
     * 保存
     *
     * @param device 设备
     * @return {@link ResponseResult}<{@link Object}>
     */
    @PostMapping
    public ResponseResult<Object> save(@RequestBody Device device) {
        return deviceService.save(device);
    }

    /**
     * 删除
     *
     * @param ids 设备id集合
     * @return {@link ResponseResult}<{@link Object}>
     */
    @DeleteMapping
    public ResponseResult<Object> remove(@RequestBody List<Long> ids) {
        return deviceService.remove(ids);
    }

    /**
     * 编辑
     *
     * @param device 设备
     * @return {@link ResponseResult}<{@link Object}>
     */
    @PutMapping
    public ResponseResult<Object> edit(@RequestBody Device device) {
        return deviceService.edit(device);
    }

    /**
     * 分页
     *
     * @param page   页码
     * @param size   数量
     * @param device 设备
     * @return {@link ResponseResult}<{@link PageInfo}<{@link Device}>>
     */
    @GetMapping("/page")
    public ResponseResult<PageInfo<Device>> page(Integer page, Integer size, Device device) {
        log.info("分页参数 {}", device);
        return deviceService.page(page, size, device);
    }

    /**
     * 根据id查询
     *
     * @param id 设备id
     * @return {@link ResponseResult}<{@link Object}>
     */
    @GetMapping("{id}")
    public ResponseResult<Object> getById(@PathVariable Long id) {
        return deviceService.getById(id);
    }

    /**
     * 根据id集合查询
     *
     * @param deviceIds 设备id
     * @return {@link ResponseResult}<{@link List}<{@link Device}>>
     */
    @GetMapping("/ids/{deviceIds}")
    public ResponseResult<List<Device>> getByIds(@PathVariable List<Long> deviceIds) {
        return deviceService.getByIds(deviceIds);
    }
}
